<?php
require_once dirname(__FILE__) . '/Connection.php';

	class CursoPDO {
		
		private $connection = NULL;
		
		public function __construct()
		{
			$this->connection = Connection::getInstance();
		}
		
		public function inserir($curso) {
			try {
				$matriz = $curso->matrizCurricular;
				$stmt=$this->connection->beginTransaction();
				$stmt=$this->connection->prepare("INSERT INTO curso(nome, modalidade_id)
												  VALUES (:nome, :modalidade_id);");
				$stmt->bindValue("nome", $curso->nome);
				$stmt->bindValue("modalidade_id", $curso->modalidade->id);
				$stmt->execute();
				$stmt = $this->connection->query("SELECT LAST_INSERT_ID() INTO @ID;");
				$stmt->execute();
				if(isset($matriz))
				{
					foreach ($matriz as $i => $value)
					{
						$stmt=$this->connection->prepare("INSERT INTO curso_professor_disciplina
													(curso_id, professor_id, disciplina_id)
												  	 VALUES (@ID, :professor->id, :discilplina_id);");
						$stmt->bindValue("professor_id", $matriz[$i]->professor->id);
						$stmt->bindValue("disciplina_id", $matriz[$i]->disciplina->id);
						$stmt->execute();
					}					
				}				
				$this->connection->commit();
			} catch (PDOException $erro) {
				echo $erro->getMessage();
				$this->connection->rollBack();			
			}
		}
	}
?>